const Sequelize = require("sequelize");

const seq = require("./seq")

//创建 User 模型
const User = seq.define("user",{
    //id会自动创建，并设为主键  自增
    userName: {
        type: Sequelize.STRING,     // 对应数据库的 varchar(255)
        allowNull: false
    },
    password: {
        type: Sequelize.STRING,
        allowNull: false
    },
    nickName: {
        type: Sequelize.STRING,
        comment: "昵称"     //这是注释内容
    }
    //  sequelize 会自动创建，createdAt 和 updatedAt
})

//创建 Blog 模型
const Blog = seq.define("blog", {
    title: {
        type: Sequelize.STRING,     
        allowNull: false
    },
    content: {
        type: Sequelize.TEXT,
        allowNull: false
   },
    userId: {
         type: Sequelize.INTEGER,     
         allowNull: false
     }
})

// 外键关联
Blog.belongsTo(User, {
    //创建外键 Blog.userId --> User.id
    foreignKey: 'userId'
})
// Blog.belongsTo(User)

User.hasMany(Blog, {
    foreignKey: 'userId'
    // 为Blog 添加userId 外键
})

module.exports = {
    User,
    Blog
}